﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SCRUM.Core.DAO;
using SCRUM.Core.BusinessObjects;

/// <summary>
/// Widget showing users in specified project/team and role.
/// </summary>
public partial class UsersInRole : WidgetBase
{
    private IList<User> users;

    /// <summary>
    /// Gets the name of the widget. It must be exactly the same as the folder that contains the widget.
    /// </summary>
    /// <value></value>
    public override string Name
    {
        get { return "UsersInRole"; }
    }

    /// <summary>
    /// Gets the title. Title is shown in the header of the widget container.
    /// </summary>
    /// <value>The title.</value>
    public override string Title
    {
        get { return (string)GetLocalResourceObject("Title"); } 
    }

    /// <summary>
    /// Gets a value indicating whether this instance is editable.
    /// </summary>
    /// <value>
    /// 	<c>true</c> if this instance is editable; otherwise, <c>false</c>.
    /// </value>
    public override bool IsEditable
    {
        get { return false; }
    }

    /// <summary>
    /// Loads all users
    /// </summary>
    private void loadUsers()
    {
        users = DAOUser.GetTeamMembers(null, null);
    }

    /// <summary>
    /// Sets handlers for events.
    /// </summary>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected override void OnInit(EventArgs e)
    {
        this.UserDetails.PasswordChangeSuccessEvent += new EventHandler(PasswordChangedSuccess);
    }

    /// <summary>
    /// This method works as a substitute for Page_Load. You should use this method for
    /// data binding etc. instead of Page_Load.
    /// </summary>
    public override void LoadWidget()
    {
        users = DAOUser.GetTeamMembers(null, null);
        //loadUsers();
        if (!this.IsPostBack)
        {
            
            GridViewData.DataSource = DAOUser.GetTeamMembers(null, null);
            GridViewData.DataBind();
        }
    }

    /// <summary>
    /// Shows the edit.
    /// </summary>
    public override void ShowEdit()
    {
        throw new NotImplementedException();
    }

    /// <summary>
    /// Hides the edit.
    /// </summary>
    public override void HideEdit()
    {
        throw new NotImplementedException();
    }

    /// <summary>
    /// Binds a data source to the invoked server control and all its child controls.
    /// </summary>
    public override void DataBind()
    {
        base.DataBind();
        LoadData();
    }

    /// <summary>
    /// Loads the data.
    /// </summary>
    protected void LoadData()
    {
        users = DAOUser.GetTeamMembers(
            this.TeamSelectionTeam.SelectedTeam, this.SingleRoleSelectionRoles.SelectedRole);
        GridViewData.DataSource = users;
        GridViewData.DataBind();
    }

    /// <summary>
    /// Applies the filter.
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void ApplyFilter(object sender, EventArgs e)
    {
        LoadData();
    }

    /// <summary>
    /// Clears the filter.
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void ClearFilter(object sender, EventArgs e)
    {
        this.TeamSelectionTeam.Clear();
        this.SingleRoleSelectionRoles.Clear();
        LoadData();
    }

    /// <summary>
    /// Called when [row data bound].
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewRowEventArgs"/> instance containing the event data.</param>
    protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        User user = e.Row.DataItem as User;
        if (user != null)
        {
            ((YesNoInfo)e.Row.FindControl("YesNo")).Value = user.Enabled;
            HyperLink emailHyperlink = (HyperLink)e.Row.FindControl("HyperlinkEmail");
            emailHyperlink.NavigateUrl = String.Format("mailto:{0}", user.Email);
            emailHyperlink.Text = user.Email;
        }
    }

    /// <summary>
    /// Create new user.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ButtonCreate_Click(object sender, EventArgs e)
    {
        this.UserDetails.SelectedItem = null;
        this.UserDetails.Visible = true;
        this.UserDetails.ShowPasswordArea();
        this.UserDetails.HideImageKey();
        this.ButtonCreateItem.Visible = true;
        this.ButtonCancelItem.Visible = true;
        //haveToShowItemDetails = true;
        this.ButtonSaveItem.Visible = false;
    }

    /// <summary>
    /// Raises when user wants to Create new User
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    public void ButtonCreateItem_Click(object sender, EventArgs e)
    {
        User newUser = UserDetails.CreateItem();
        DAODashboardLayout.CreateDefaultLayout(newUser);
        //int profileUserID = DAOUser.GetUserProfileID(newUser.DefaultRoleID);
        //DAODashboardLayout.CopyDashboardLayout(profileUserID, newUser.ID);
        //CreateDefaultLayout(newUser);
        this.UserDetails.Visible = false;
        this.ButtonCreateItem.Visible = false;
        this.ButtonCancelItem.Visible = false;
    }

    /// <summary>
    /// Raises when user wants to Save existing User
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    public void ButtonSaveItem_Click(object sender, EventArgs e)
    {
        UserDetails.EditItem(UserDetails.SelectedItem.ID);
        this.UserDetails.Visible = false;
        this.ButtonCreateItem.Visible = false;
        this.ButtonCancelItem.Visible = false;
    }

    /// <summary>
    /// Raises when user wants to cancel editting User
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    public void ButtonCancelItem_Click(object sender, EventArgs e)
    {
        this.UserDetails.Visible = false;
    }

    /// <summary>
    /// Raises when user wants to delete existing User
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    public void ButtonDeleteItem_Click(object sender, EventArgs e)
    {
        UserDetails.DeleteItem(UserDetails.SelectedItem);
        this.UserDetails.Visible = false;
    }
    
    /// <summary>
    /// User click on some user in Gridview
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void GridViewData_SelectedIndexChanged(object sender, EventArgs e)
    {
        SelectUser(GridViewData.SelectedIndex);
        UserDetails.Visible = true;
        //SelectSprint(GridViewData.SelectedIndex);
        //haveToShowItemDetails = SprintDetail.SelectedItem != null;
        //this.ButtonCreateItem.Visible = false;
    }

    /// <summary>
    /// Selects the sprint specified by position in all sprints
    /// Displays sprint details, shows/hides delete button
    /// </summary>
    /// <param name="sprintPos">The sprint pos.</param>
    private void SelectUser(int userPos)
    {
        this.UserDetails.SelectedItem = (userPos == -1) ? null : users[userPos];
        UserDetails.Visible = true;
        this.ButtonSaveItem.Visible = true;
        this.ButtonDeleteItem.Visible = true;
        this.ButtonCancelItem.Visible = true;
        //haveToShowItemDetails = UserDetails.SelectedItem != null;
    }

    /// <summary>
    /// Handles the PreRender event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    //protected void Page_PreRender(object sender, EventArgs e)
    //{
    //    if (this.GridViewData.DataSource == null)//(this.GridViewData.DataSource as IList<User>).Count == 0)
    //    {
    //        this.ShowOperationResult(MessageType.Warning, "no_data_for_filters");
    //    }
    //}

    /// <summary>
    /// Handles event that password was changed successfuly
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void PasswordChangedSuccess(object sender, EventArgs e)
    {
        this.ShowOperationResult(MessageType.Info, "Password");
    }    
}
